using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using SharedContent;

namespace GlobalGameJam
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager   graphics;
        ContentManager          content;
        SpriteBatch             spriteBatch;
        Gravite                 g;
        Player                  player;
        public List<Plateform>               plateforms;
        public CollisionManager collisionManager;
        
        public Game1()
        {
            graphics    = new GraphicsDeviceManager(this);
            content     = new ContentManager(Services);
            content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth  = 1200;
            graphics.PreferredBackBufferHeight = 800;
        
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.player = new Player(this);
            collisionManager = new CollisionManager();
            this.g = new Gravite(player, plateforms, this);
            base.Initialize();
        }

   
        
        public void Collisions(GameTime gametime)
        {
            this.g.checkGravite(gametime);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D textPlayer = this.content.Load<Texture2D>("soloSprite");
            Texture2D textDown = this.content.Load<Texture2D>("estomac1/down");
            this.player.Initialize(textPlayer, Vector2.Zero, new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));

            Plateform toAdd;
            Texture2D textToAff;

            for (int i = 0; i < 6; i++)
            {
                toAdd = new Plateform(this);
                textToAff = this.content.Load<Texture2D>("estomacv2/part" + (i + 1));
               
                toAdd.Initialize(textToAff, new Vector2(i * 200, graphics.PreferredBackBufferHeight - textToAff.Height), new Vector2(0 + i * 200, graphics.PreferredBackBufferHeight - textToAff.Height), new Vector2(0, 0));
                collisionManager.plateforms.Add(toAdd);
            }

            Bubble toAdd2 = new Bubble(this);


            for (int i = 0; i < 6; i++)
            {
                 toAdd = new Plateform(this);
                 textToAff = this.content.Load<Texture2D>("estomacv2/L51" + (i + 1));
             
                toAdd.Initialize(textToAff, new Vector2(i * 200, 0 ), new Vector2(i * 200, 0), new Vector2(0, 0));
                collisionManager.plateforms.Add(toAdd);
            }
     
            Plateform toAdd3 = new Plateform(this);
            Texture2D textToAff3 = this.content.Load<Texture2D>("estomac1_bords");
            toAdd3.Initialize(textToAff3, new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(1.07f, 0.9f));
            collisionManager.plateforms.Add(toAdd3);

            toAdd = new Bubble(this);

            textToAff = this.content.Load<Texture2D>("bubbles/bull1");
            toAdd2.Initialize(textToAff, new Vector2(0, 400), new Vector2(600, 400), new Vector2(1, 0));
            toAdd2.setPopping(this.content.Load<Texture2D>("bubbles/bull2"));
            toAdd2.setPop(this.content.Load<Texture2D>("bubbles/bull3"));
            collisionManager.plateforms.Add(toAdd2);
            this.player.LoadContent(this.content, "inputManager");
        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            KeyboardState state = Keyboard.GetState();

            if (state.IsKeyDown(Keys.N))
            {
                if (this.g.direction == 0)
                    this.g.setDir(1);
                else
                    this.g.setDir(0);
            }

            if (state.IsKeyDown(Keys.D))
            {
                player.position.X = 100;
                player.position.Y = 200;
                player.UpdateHitbox();
            }

            foreach (Plateform p in collisionManager.plateforms)
                p.Update(gameTime);
            // TODO: Add your update logic here
            this.player.Update(gameTime);
            this.Collisions(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Pink);

            // TODO: Add your drawing code here
            
            foreach (Plateform p in collisionManager.plateforms)
                p.Draw(gameTime, spriteBatch);
            this.player.Draw(gameTime, spriteBatch);
           
            base.Draw(gameTime);
        }
    }
}
